/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelController.applicationController;

import entities.Knowledge;
import entities.Myidea;
import entities.TeacherAdmin;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Named;
import sessionBeans.MyideaFacadeLocal;
import tools.StaticFields;


/**
 *
 * @author Administrator
 */
@Named("myideaControllerA")
@ApplicationScoped
public class MyideaController {
     @EJB
    private sessionBeans.MyideaFacadeLocal ejbFacadelocal;
    
    private List<Myidea> myideaList = null;

    public MyideaController() {
    }

    private MyideaFacadeLocal getFacade() {
        return ejbFacadelocal;
    }

    public Myidea getLearningresource(java.lang.Integer id) {
        return ejbFacadelocal.find(id);
    }

    public List<Myidea> getMyideaList() {
        if (null == myideaList || myideaList.isEmpty()) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, StaticFields.DAY_NEWS);
            this.myideaList = getFacade().getQueryResultList("select * from news where inputdate<DATE('"
                    + calendar.get(Calendar.YEAR) + "-"
                    + calendar.get(Calendar.MONTH) + "-"
                    + calendar.get(Calendar.DAY_OF_MONTH)
                    + "')order by inputedate");
        }
        return myideaList;
    }

    public Set<Myidea> getMyidea4Teacher(TeacherAdmin teacher) {
        return teacher.getMyideaSet();
    }

    public Myidea getMyidea(java.lang.Integer id) {
        return ejbFacadelocal.find(id);
    }

    public void create(Myidea news) {
        getFacade().create(news);
    }

    public void remove(Myidea news) {
        getFacade().remove(news);
    }

    public void edit(Myidea news) {
        getFacade().edit(news);
    }

    public List<Myidea> getQueryResultList(String sql) {
        return getFacade().getQueryResultList(sql);
    }

    public int count() {
        return getFacade().count();
    }

    public void executUpdate(String updateString) {
        getFacade().executUpdate(updateString);
    }

    public List<Myidea> findRange(int[] range) {
        return getFacade().findRange(range);
    }

    public Myidea find(Integer id) {
        return getFacade().find(id);
    }
    


    @FacesConverter(forClass = Myidea.class)
    public static class MyideaControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            MyideaController controller = (MyideaController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "myIdeaControllerA");
            return controller.getMyidea(Integer.valueOf(value));
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Myidea) {
                Myidea o = (Myidea) object;
                return String.valueOf(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Myidea.class.getName());
            }
        }
    }
}
